package com.nikmesoft.android.alwaysontime.xml;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.nikmesoft.android.alwaysontime.beans.Employee;

public class SAXXMLHandler extends DefaultHandler {

	private List<Employee> employees;
	private String tempVal;
	private Employee tempEmp;

	public SAXXMLHandler() {
		employees = new ArrayList<Employee>();
	}

	public List<Employee> getEmployees() {
		return employees;
	}
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		tempVal = "";
		if (qName.equalsIgnoreCase("employee")) {
			tempEmp = new Employee();
		}
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		tempVal = new String(ch, start, length);
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equalsIgnoreCase("employee")) {
			employees.add(tempEmp);
		} else if (qName.equalsIgnoreCase("id")) {
			tempEmp.setId(Integer.parseInt(tempVal));
		} else if (qName.equalsIgnoreCase("name")) {
			tempEmp.setName(tempVal);
		} else if (qName.equalsIgnoreCase("type")) {
			tempEmp.setType(tempVal);
		} 
	}

}
